package com.astoolkit.commons.databinding
{
	import com.astoolkit.commons.reflection.ObjectPropertiesUtil;
	import com.astoolkit.commons.reflection.PropertyInfo;
	
	import flash.utils.getDefinitionByName;

	public class BindingUtility
	{
		public  static function enableAllBindings( inOwner : Object, inTarget : Object ) : void
		{
			switchAllBindings( inOwner, inTarget, true );
		}
		
		public  static function disableAllBindings( inOwner : Object, inTarget : Object ) : void
		{
			switchAllBindings( inOwner, inTarget, false );
		}
		
		private static function switchAllBindings( inOwner : Object, inTarget : Object, inEnable : Boolean ) : void
		{
			var bindingManager : Object = getDefinitionByName( "mx.binding.BindingManager" );
			var theId : PropertyInfo;
			for each( theId in ObjectPropertiesUtil.getBindableObjectProperties( inOwner ) )
			{
				if( theId.readable && theId.writable && inOwner.hasOwnProperty( theId.name ) && inOwner[ theId.name ] == inTarget )
					break;
			}
			if( theId )
			{
				for each(var p : PropertyInfo in ObjectPropertiesUtil.getBindableObjectProperties( inTarget ) )
				{
					bindingManager.enableBindings( inOwner, theId.name + "." + p.name, inEnable );
					if( inEnable )
						bindingManager.executeBindings( inOwner, theId.name + "." + p.name, inTarget );
				}
			}

		}
	}
}